from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from utils.modelsMixin import ModelSetMixin, DateTimeModelMixin


class Classification(ModelSetMixin):
    name = models.CharField(max_length=40, verbose_name="分类名称")
    parent = models.ForeignKey("self", on_delete=models.SET_NULL, null=True, blank=True, verbose_name="父级分类")

    class Meta:
        verbose_name = "商品分类"
        # 复数形式 英文里面的复数形式在单词的后面加 s
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s.%s" % (self.id, self.name)


# 商品模型
class Commodity(ModelSetMixin):
    # 商品发布状态
    STATUS_CHOICES = (
        (0, "下架"),
        (1, "上架")
    )
    name = models.CharField(max_length=100, verbose_name="商品名称")
    caption = models.TextField(verbose_name="商品副标题")
    brand = models.CharField(max_length=100, null=True, blank=True, verbose_name="商品品牌")
    # Decimal 高度计算 max_digits=10 加上小数位最大位数10位, decimal_places=2 小数位最大两位
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品价格")
    stock = models.IntegerField(verbose_name="商品库存")
    pack = models.TextField(null=True, blank=True, verbose_name="商品包装信息")
    serviceafter_sale = models.TextField(null=True, blank=True, verbose_name="售后服务", help_text="商品的售后服务说明")
    sales = models.IntegerField(default=0, verbose_name="商品销量")
    comments = models.IntegerField(default=0, verbose_name="商品评价数")
    status = models.IntegerField(default=0, verbose_name="商品状态", choices=STATUS_CHOICES)
    detail = models.TextField(null=True, blank=True, verbose_name="商品详情")
    classification1 = models.ForeignKey(Classification, on_delete=models.PROTECT, verbose_name='一级分类',
                                        related_name="classification1")
    classification2 = models.ForeignKey(Classification, on_delete=models.PROTECT, verbose_name='二级分类',
                                        related_name="classification2")

    class Meta:
        # 排序
        ordering = ['-sales', '-comments', 'create_time']
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 商品图片模型
class CommodityImg(ModelSetMixin):
    src = models.TextField(verbose_name="图片地址")
    priority = models.IntegerField(default=0, verbose_name="优先级")
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name="商品")

    class Meta:
        ordering = ['-priority']
        verbose_name = '商品图片'
        verbose_name_plural = verbose_name


# 购物车模型
class ShoppingCart(DateTimeModelMixin):
    number = models.IntegerField(default=1, verbose_name="商品数量")
    commodity = models.ForeignKey(Commodity, on_delete=models.CASCADE, verbose_name="商品")
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")

    class Meta:
        ordering = ['-create_time']
        verbose_name = '购物车'
        verbose_name_plural = verbose_name


# 订单模型
class Order(DateTimeModelMixin):
    PAY_METHOD_CHOICES = (
        (0, "支付宝"),
        (1, "微信"),
        (2, "货到付款")
    )
    STATUS_CHOICES = (
        (0, "待付款"),
        (1, "待发货"),
        (2, "待收货"),
        (3, "待评价"),
        (4, "已完成"),
        (5, "已取消")
    )
    order_id = models.CharField(max_length=100, primary_key=True, verbose_name="订单编号")
    total_count = models.IntegerField(default=1, verbose_name="商品数量")
    total_amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品总价")
    pay_method = models.IntegerField(default=0, verbose_name="支付方式", choices=PAY_METHOD_CHOICES)
    status = models.IntegerField(default=0, verbose_name="订单状态", choices=STATUS_CHOICES)
    address = models.TextField(verbose_name="收货地址")
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name="用户")

    class Meta:
        ordering = ['-create_time']
        verbose_name = '订单表'
        verbose_name_plural = verbose_name


# 订单商品模型
class OrderGoods(DateTimeModelMixin):
    number = models.IntegerField(default=1, verbose_name="商品数量")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="商品单价")
    # comments = models.IntegerField(default=0, verbose_name="商品评价数", null=True, blank=True)
    # score = models.IntegerField(default=0, verbose_name="商品评分", null=True, blank=True)
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name="订单")
    commodity = models.ForeignKey(Commodity, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="商品")

    class Meta:
        db_table = 'order_goods'
        ordering = ['-create_time']
        verbose_name = '订单商品'
        verbose_name_plural = verbose_name


# 支付模型
class Payment(DateTimeModelMixin):
    trade_id = models.CharField(max_length=100, unique=True, verbose_name="支付宝交易号")
    order = models.ForeignKey(Order, on_delete=models.CASCADE, verbose_name="订单")

    class Meta:
        ordering = ['-create_time']
        verbose_name = '支付信息'
        verbose_name_plural = verbose_name
